package com.example.scaffolding.composer

import android.content.Context
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import androidx.test.core.app.ApplicationProvider
import com.example.scaffolding.util.EventListener
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException

class ImageComposerTest {
    private lateinit var imageComposer: ImageComposer
    private lateinit var countDownLatchSuccess: CountDownLatch
    private lateinit var countDownLatchError: CountDownLatch
    private lateinit var context: Context

    companion object {
        const val RESULT_SUCCESS = "success"
        const val RESULT_ERROR = "error"
    }

    @get:Rule
    var instantExecutorRule = InstantTaskExecutorRule()

    @Before
    fun setUp() {
        imageComposer = ImageComposer()
        context = ApplicationProvider.getApplicationContext<Context>()
        countDownLatchSuccess = CountDownLatch(2)
        countDownLatchError = CountDownLatch(2)
    }

    @After
    fun tearDown() {
    }

    @Test
    fun testaddInput() {
        val urls = arrayOf("https://img-blog.csdnimg.cn/20181030124822734.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMTUzNDc4,size_16,color_FFFFFF,t_70",
            "https://img-blog.csdnimg.cn/2018103012495363.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMTUzNDc4,size_16,color_FFFFFF,t_70")
        val results = mutableListOf<String>()
        val eventListener = object : EventListener<String> {
            override fun onProgress(progress: Int) {
            }

            override fun onSuccess(t: String) {
            }

            override fun onError(e: Exception) {
            }
        }
        urls.forEach {
            imageComposer.addInput(it, eventListener)
        }
        val liveDatas = imageComposer.createWork(context)
        liveDatas[0].observeForever {
            if (it.state.isFinished) {
                countDownLatchSuccess.countDown()
                it.outputData.getString(KEY_RESULT)?.let { content ->
                    results.add(content)
                }

            }
        }
        liveDatas[1].observeForever {
            if (it.state.isFinished) {
                countDownLatchSuccess.countDown()
                it.outputData.getString(KEY_RESULT)?.let { content ->
                    results.add(content)
                }

            }
        }
        liveDatas[2].observeForever {
            if (it.state.isFinished) {
                countDownLatchSuccess.countDown()
                it.outputData.getString(KEY_BITMAP_RESULT)?.let { content ->
                    results.add(content)
                }

            }
        }
        liveDatas[3].observeForever {
            if (it.state.isFinished) {
                countDownLatchSuccess.countDown()
                it.outputData.getString(KEY_BITMAP_RESULT)?.let { content ->
                    results.add(content)
                }

            }
        }
        if (!countDownLatchSuccess.await(60, TimeUnit.SECONDS)) {
            throw TimeoutException("ImageComposer coast too much time")
        }
        assert(liveDatas.size == 4)

    }


}